/*
v 1.5
Simple client Validation Plugin 

- require jquery UI
- by tiger

*/

(function($) {

    jQuery.fn.twValidate = function(settings) {
        var defaults = { 
						charMes: "Please type character only" , 
						charThMes:"กรุณากรอกอักษรไทยเท่านั้น",
						charEnMes:"Please type a-z character only",
						numMes: "Please type numberic only" , 
						symbolMes: "Please don't type any symbol" ,
						emailMes: "Wrong format of e-mail" ,
						requireMes: "Please complete this filed" ,
						lengthMes: "Length of this filed not least than " ,
						customClass: "customValidate" ,
						customReq: null ,
						customMes: "This is custom message" ,
						customType: false,
						inLine:false,
						noDiv:false,
						callBack: false,
						checkAllBy : false
						}; //removeAble : true, false, 'auto'
        var options = jQuery.extend(defaults, settings);
		
        $("body").find('div.validateMes').remove(); //remove old
		
		$(this).find(".validate").each(function(){
				//
				$(this).unbind("focus").unbind("blur").unbind("keyup")
				//
				var notAccept = null;
				var Accept = null;
				var MES;
				
				if($(this).hasClass("char")){
					notAccept = /([0-9])|([\`\~\!\@\#\%\&\+\-\=\^\.\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])/;//char
					MES = options.charMes;
				}else if($(this).hasClass("charTH")){
					notAccept = /[A-z0-9]|^\.|([\`\~\!\@\#\%\&\+\-\=\^\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])|((\.\d*){2,})|\s/;
					MES = options.charThMes;
				}else if($(this).hasClass("charEN")){
					notAccept = /[ก-ฮะ-แ0-9]|^\.|([\`\~\!\@\#\%\&\+\-\=\^\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])|((\.\d*){2,})|\s/;
					MES = options.charEnMes;
				}else if($(this).hasClass("number")){
					notAccept = /\D/;
					MES = options.numMes;
				}else if($(this).hasClass("symbol")){
					notAccept = /[\`\~\!\@\#\%\&\+\-\=\^\.\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;]/;//symbol
					MES = options.symbolMes;
				}
				else if($(this).hasClass("email")){
					//notAccept = /^@|@+\.|@$|[\<\>\<\>\\\[\]\(\)\"\'\:\;]+|@[\.\<\>\<\>\\\[\]\(\)\"\'\:\;]+|\.\w{1}$|\w{1}\.$|@\w+$|\s/; //email 
					Accept = /^[\w-]+(\.[\w-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/;
					MES = options.emailMes;
				}else if($(this).hasClass(options.customClass)){
					//
					if(options.customType == "accept"){
						Accept = options.customReq; //custom 
					}else{
						notAccept = options.customReq; //custom 
					}
					MES = options.customMes;
				}
				//if has value
				if($(this).val() != ""){
					if(notAccept != null){
						checkNotAccept($(this).val(),notAccept,MES,$(this));
					}else if(Accept != null){
						checkAccept($(this).val(),Accept,MES,$(this));
					}
				}
				//check on key
				$(this).keyup(function(){
					if(notAccept != null){
						checkNotAccept($(this).val(),notAccept,MES,$(this));
					}else if(Accept != null){
						checkAccept($(this).val(),Accept,MES,$(this));
					}
				});
				//map to ui-require-field
				$(this).blur(function(){
					//
					if(notAccept != null){
						checkNotAccept($(this).val(),notAccept,MES,$(this));
					}else if(Accept != null){
						checkAccept($(this).val(),Accept,MES,$(this));
					}
					//$(this).next("div.validateMes").remove();
					if($(this).hasClass("ui-require-field")){
						if(!$(this).val()){
							$(this).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px;"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + options.requireMes + '</div></div>');
						}
					}
					//
					if(options.inLine){fixInline($(this))}else if(options.noDiv && $(this).hasClass("ui-require-field")){fixNoDiv($(this),options.requireMes)};
				}).focus(function(){
					//alert("focus")
					$(this).css("background-color","white").removeAttr("title").next("div.validateMes").remove();
				});
		
		});
		//check not accept
		function checkNotAccept(thisText,notAccept,MES,obj){
			if (notAccept.test(thisText)) { 
				if(options.callBack){eval(options.callBack)}//call back function
				$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px;"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + MES + '</div></div>');
				if(options.inLine){fixInline(obj)}else if(options.noDiv){fixNoDiv(obj,MES)};
				return false;
			}else{
				//check lenght
				if($(obj).attr("minlength") && thisText.length < $(obj).attr("minlength") && thisText != ""){
					$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px;"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + options.lengthMes + $(obj).attr("minlength") +'</div></div>');
					if(options.inLine){fixInline(obj)}else if(options.noDiv){fixNoDiv(obj,(options.lengthMes + $(obj).attr("minlength")))};
				}else{
					$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove();
				}
			}
			//
		}
		//check accept
		function checkAccept(thisText,Accept,MES,obj){
			//
			if (!Accept.test(thisText) && thisText != "") { 
				if(options.callBack){eval(options.callBack)}//call back function
				$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px;"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + MES + '</div></div>');
				if(options.inLine){fixInline(obj)}else if(options.noDiv){fixNoDiv(obj,MES)};
				return false;
			}else{
				//check lenght
				if($(obj).attr("minlength") && thisText.length < $(obj).attr("minlength")){
					$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px; margin-top:2px"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + options.lengthMes + $(obj).attr("minlength") +'</div></div>');
					if(options.inLine){fixInline(obj)}else if(options.noDiv){fixNoDiv(obj,(options.lengthMes + $(obj).attr("minlength")))};
				}else{
					$(obj).css("background-color","white").removeAttr("title").next("div.validateMes").remove();
				}
			}
			
		}
		//check if inline display
		function fixInline(obj){
			$(obj).next("div.validateMes").css({"position":"absolute","top":($(obj).position().top+$(obj).height()+4),"left":($(obj).position().left),"width":$(obj).width()-6,"z-index":10}).addClass("ui-corner-bottom");
		}
		//check if no div display
		function fixNoDiv(obj,MES){
			if($(obj).next("div.validateMes").size()){
				$(obj).css({"background-color":"#FFD8E0"}).attr("title",MES).next("div.validateMes").hide();
			}
		}
//-----------
		if(options.checkAllBy){
		var checkAt = $(this);
			$(options.checkAllBy).click(function(){
												 
				//
				$(checkAt).find(".validate").unbind("focus").each(function(){
					var notAccept = null;
					var Accept = null;
					var MES;
					
					if($(this).hasClass("char")){
						notAccept = /([0-9])|([\`\~\!\@\#\%\&\+\-\=\^\.\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])/;//char
						MES = options.charMes;
					}else if($(this).hasClass("charTH")){
						notAccept = /[A-z0-9]|^\.|([\`\~\!\@\#\%\&\+\-\=\^\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])|((\.\d*){2,})|\s/;
						MES = options.charThMes;
					}else if($(this).hasClass("charEN")){
						notAccept = /[ก-ฮะ-แ0-9]|^\.|([\`\~\!\@\#\%\&\+\-\=\^\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;])|((\.\d*){2,})|\s/;
						MES = options.charEnMes;
					}else if($(this).hasClass("number")){
						notAccept = /\D/;
						MES = options.numMes;
					}else if($(this).hasClass("symbol")){
						notAccept = /[\`\~\!\@\#\%\&\+\-\=\^\.\,\_\$\*\?\{\}\<\>\/\\\|\[\]\(\)\"\'\:\;]/;//symbol
						MES = options.symbolMes;
					}
					else if($(this).hasClass("email")){
						//notAccept = /^@|@+\.|@$|[\<\>\<\>\\\[\]\(\)\"\'\:\;]+|@[\.\<\>\<\>\\\[\]\(\)\"\'\:\;]+|\.\w{1}$|\w{1}\.$|@\w+$|\s/; //email 
						Accept = /^[\w-]+(\.[\w-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/;
						MES = options.emailMes;
					}else if($(this).hasClass(options.customClass)){
						//
						if(options.customType == "accept"){
							Accept = options.customReq; //custom 
						}else{
							notAccept = options.customReq; //custom 
						}
						MES = options.customMes;
					}
					//
					if(notAccept != null){
						checkNotAccept($(this).val(),notAccept,MES,$(this));
					}else if(Accept != null){
						checkAccept($(this).val(),Accept,MES,$(this));
					}				
					//
					//$(this).next("div.validateMes").remove();
					if($(this).hasClass("ui-require-field")){
						if(!$(this).val()){
							$(this).css("background-color","white").removeAttr("title").next("div.validateMes").remove().end().after('<div class="validateMes ui-state-highlight ui-helper-clearfix" style="padding:4px;"><div class="ui-icon ui-icon-info" style="float:left"></div><div class="valMes" style="float:left">' + options.requireMes + '</div></div>');
						}
					}
					//
				});
				//
			});
		}
//---------------
    }

})(jQuery);